import 'dart:math' as math;

import 'package:flutter/material.dart';
import 'package:media_kit/media_kit.dart';
import 'package:qh_music/src/shared/helpers/tools.dart';
import 'package:qh_music/src/widgets/audio/volume_mute_controller.dart';

class VolumeSliderControllerWidget extends StatelessWidget {
  final Player player;
  final double width;
  final double height;

  const VolumeSliderControllerWidget(
      {super.key, required this.player, this.width = 200, this.height = 48});

  @override
  Widget build(BuildContext context) {
    return Transform.rotate(
      angle: getRotateAngle(270),
      child: Card(
        margin: const EdgeInsets.all(0),
        child: SizedBox(
          width: width,
          height: height,
          child: Row(
            children: [
              buildMuteController(),
              buildVolumeCoreController(),
              buildVolumeText(),
            ],
          ),
        ),
      ),
    );
  }

  Widget buildVolumeText() {
    return Transform.rotate(
      angle: getRotateAngle(90),
      child: SizedBox(
        width: 32,
        height: 20,
        child: Center(
          child: StreamBuilder(
            stream: player.stream.volume,
            builder: (context, snapshot) => Text(
              (snapshot.data ?? player.state.volume).toInt().toString(),
              style: Theme.of(context).textTheme.labelMedium,
            ),
          ),
        ),
      ),
    );
  }

  Widget buildVolumeCoreController() {
    return Expanded(
      child: Container(
        decoration: const BoxDecoration(
          border: Border.symmetric(
            vertical: BorderSide(color: Colors.black12),
          ),
        ),
        child: StreamBuilder(
          stream: player.stream.volume,
          builder: (context, snapshot) => SliderTheme(
            data: SliderTheme.of(context).copyWith(
              thumbShape: const RoundSliderThumbShape(
                enabledThumbRadius: 6,
                pressedElevation: 0,
                elevation: 0,
              ),
              trackShape: CustomTrackShape(),
            ),
            child: Slider(
              value: snapshot.data ?? player.state.volume,
              min: 0,
              max: 100,
              divisions: 100,
              onChanged: (value) {
                player.setVolume(value);
              },
            ),
          ),
        ),
      ),
    );
  }

  Widget buildMuteController() {
    return Transform.rotate(
      angle: getRotateAngle(90),
      child: VolumeMuteControllerWidget(
        player: player,
      ),
    );
  }
}

// 自定义轨道形状，减小两侧空白
class CustomTrackShape extends RoundedRectSliderTrackShape {
  /// 两端间距
  final double _trackGap = 12;

  @override
  Rect getPreferredRect({
    required RenderBox parentBox,
    Offset offset = Offset.zero,
    required SliderThemeData sliderTheme,
    bool isEnabled = false,
    bool isDiscrete = false,
  }) {
    final double trackHeight = sliderTheme.trackHeight!;
    assert(trackHeight >= 0);

    final double trackLeft = offset.dx + _trackGap;
    final double trackTop =
        offset.dy + (parentBox.size.height - trackHeight) / 2;
    final double trackRight = trackLeft + parentBox.size.width - _trackGap * 2;
    final double trackBottom = trackTop + trackHeight;
    // If the parentBox'size less than slider's size the trackRight will be less than trackLeft, so switch them.
    return Rect.fromLTRB(math.min(trackLeft, trackRight), trackTop,
        math.max(trackLeft, trackRight), trackBottom);
  }
}
